import { VuexModule, Module, Action, getModule } from 'vuex-module-decorators'
import { getRoles, updateRole, deleteRole, getRoleMenus, updateRoleMenus, deleteRoleMenus } from '@/api/roles'
import store from '@/store'
import { ISysRoleModel } from '@/api/types'

export interface IRolesState {

}

@Module({ dynamic: true, store, name: 'roles' })
class Roles extends VuexModule implements IRolesState {

    @Action
    public async getRoles() {
        return new Promise<Array<ISysRoleModel>>((r, j) => {
            getRoles({}).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                j([])
            })
        })
    }

    @Action
    public async updateRole(data: any) {
        return new Promise((r, j) => {
            updateRole(data).then((value: any) => {
                console.error(value)
                r(value.data)
            }).catch((ex: any) => {
                console.error(ex);
                j([]);
            })
        })
    }

    @Action
    public async deleteRole(roleId: number) {
        return new Promise((r, j) => {
            deleteRole(roleId).then((value: any) => {
                console.error(value)
                r(value.data)
            }).catch((ex: any) => {
                console.error(ex);
                j([]);
            })
        })
    }

}

export const RolesModule = getModule(Roles)
